package huajiu.com.controller;

import huajiu.com.base.ResultEntity;
import huajiu.com.base.StringUtil;
import huajiu.com.domain.DoctorSolution;
import huajiu.com.domain.ToningSolution;
import huajiu.com.service.DoctorSolutionService;
import huajiu.com.service.ToningSolutionService;
import huajiu.com.vo.DoctorSolutionVo;
import huajiu.com.vo.SolutionVo;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ToningSolutionController {

	@Autowired
	private ToningSolutionService toningSolutionService;

	@Autowired
	private DoctorSolutionService doctorSolutionService;

	@ResponseBody
	@RequestMapping(value = "/query.do", method = RequestMethod.GET)
	public List<SolutionVo> saveSolution() {

		ResultEntity res = new ResultEntity();
		List<SolutionVo> listVos = null;
		try {
			listVos = toningSolutionService.querySolutionVos();
			res.setData(listVos);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorCode(200);
			res.setErrorMessage(e.getMessage());
		}

		return listVos;
	}

	@ResponseBody
	@RequestMapping(value = "/ShowToningSolution.do", method = RequestMethod.GET)
	public List<ToningSolution> showSolution(@RequestParam String corporeity) throws UnsupportedEncodingException {
		List<ToningSolution> toningSolutionList = new ArrayList();
		corporeity = new String (corporeity.getBytes("ISO-8859-1"),"utf-8");
		ToningSolution cortoningSolution = toningSolutionService.getCorToningSolution(corporeity);
		toningSolutionList.add(cortoningSolution);
		return toningSolutionList;
	}
	
	@ResponseBody
	@RequestMapping(value = "/ShowDoctorSolution.do", method = RequestMethod.GET)
	public DoctorSolutionVo showSolution()  {

		DoctorSolutionVo doctorSolutionVo = new DoctorSolutionVo();
		
		return doctorSolutionVo;
	}

	@ResponseBody
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	public ResultEntity saveSolution(@RequestBody SolutionVo solutionVo) {

		ResultEntity res = new ResultEntity();

		try {
			DoctorSolution dSolution = doctorSolutionService
					.isThere(solutionVo.getDoctor());
			ToningSolution toningSolution = new ToningSolution();
			if("no".equals(dSolution.getUuid())){
				String uuid = StringUtil.getUUID();
				String nowTimeString = StringUtil.now();
				DoctorSolution doctorSolution = new DoctorSolution();
				doctorSolution.setDoctor(solutionVo.getDoctor());
				doctorSolution.setUuid(uuid);
				doctorSolution.setCreateTime(nowTimeString);
				doctorSolutionService.save(doctorSolution);
				
				toningSolution.setDoctorSolutionUuid(uuid);
			}else{
				toningSolution.setDoctorSolutionUuid(dSolution.getUuid());
			}
			toningSolution.setCorporeity(solutionVo.getCorporeity());
			toningSolution.setPerformance(solutionVo.getPerformance());
			toningSolution.setTendency(solutionVo.getTendency());
			toningSolution.setToningSolutions(solutionVo.getToningSolutions());
			toningSolutionService.save(toningSolution);
			res.setData(true);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorCode(200);
			res.setErrorMessage(e.getMessage());
		}

		return res;
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultEntity updateSolution(
			@RequestBody ToningSolution toningSolution) {

		ResultEntity res = new ResultEntity();
		try {
			toningSolutionService.update(toningSolution);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorCode(200);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}

	@ResponseBody
	@RequestMapping(value = "/delete.do", method = RequestMethod.GET)
	public ResultEntity deleteSolution(@RequestParam int id) {
		ResultEntity res = new ResultEntity();
		try {
			toningSolutionService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorCode(200);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}

	@ResponseBody
	@RequestMapping(value = "/querySolution.do", method = RequestMethod.GET)
	public ResultEntity selectSolution(@RequestParam int id) {
		ResultEntity res = new ResultEntity();
		try {
			SolutionVo solutionVo = toningSolutionService.query(id);
			res.setData(solutionVo);
		} catch (Exception e) {
			e.printStackTrace();
			res.setErrorCode(200);
			res.setErrorMessage(e.getMessage());
		}
		return res;
	}
}
